package com.wedo.mall.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.e.library.fragment.EFragment;
import com.e.library.listener.EListener;
import com.e.library.utils.EViewUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.main.BannerActivity;
import com.wedo.mall.activity.MainActivity;
import com.wedo.mall.activity.main.CategoryActivity;
import com.wedo.mall.activity.main.ScannerActivity;
import com.wedo.mall.activity.main.SearchActivity;
import com.wedo.mall.adapter.CategoryAdapter;
import com.wedo.mall.adapter.FavourableAdapter;
import com.wedo.mall.adapter.RecommendAdapter;
import com.wedo.mall.adapter.ShopAdapter;
import com.wedo.mall.entity.Banner;
import com.wedo.mall.entity.Category;
import com.wedo.mall.entity.Favourable;
import com.wedo.mall.entity.Recommend;
import com.wedo.mall.entity.Shop;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;

/**
 * Created by lxy on 2017/6/8.
 */

public class MainFragment extends EFragment<MainActivity> {

    @BindView(R.id.cb_banner) ConvenientBanner cb_banner;
    @BindView(R.id.rv_category) RecyclerView rv_category;
    @BindView(R.id.rv_recommend) RecyclerView rv_recommend;
    @BindView(R.id.rv_shop) RecyclerView rv_shop;
    @BindView(R.id.rv_favourable) RecyclerView rv_favourable;
    @BindView(R.id.iv_nearby) ImageView iv_nearby;

    public static MainFragment with(){
        return new MainFragment();
    }
    @Override
    protected int layout() {
        return R.layout.fragment_main;
    }

    @Override
    protected void onFragmentCreated(View contentView, Bundle bundle) {

        String text = "乔治通知步行者明夏离队 有望加盟骑士？\n" +
                "·步行者开交易乔治条件 湖人两套方案可得到乔治\n" +
                "·苏群：若詹姆斯转投洛杉矶 湖人和快船会去哪？\n" +
                "·美媒:周琦两优点契合火箭 麦蒂:多队看好郭艾伦\n" +
                "·多支球队有意引进火箭悍将 爵士忧心两核心离开\n" +
                "·ESPN：库里对勇士价值超KD 第二支勇士将诞生？\n" +
                "·若乔治取代乐福骑士将夺冠？ 勇士最强杀招无解";
        final List<Banner> banners = new ArrayList<>();
        Banner banner1 = new Banner();
        banner1.img = "http://web.wubaiyi.com/wby/0188/images/banner.jpg";
        banner1.txt =text;

        Banner banner2 = new Banner();
        banner2.img = "http://img4.imgtn.bdimg.com/it/u=3490027345,274543905&fm=21&gp=0.jpg";
        banner2.txt =text;

        Banner banner3 = new Banner();
        banner3.img = "http://picapi.ooopic.com/11/73/23/20b1OOOPICdd.jpg";
        banner3.txt =text;

        Banner banner4 = new Banner();
        banner4.img = "http://img4.imgtn.bdimg.com/it/u=3490027345,274543905&fm=21&gp=0.jpg";
        banner4.txt =text;

        banners.add(banner1);
        banners.add(banner2);
        banners.add(banner3);
        banners.add(banner4);

        cb_banner.setPages(new CBViewHolderCreator() {
            @Override
            public Object createHolder() {
                return new BannerHolder();
            }
        },banners);
        cb_banner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Banner banner = banners.get(position);
                Bundle extras = new Bundle();
                extras.putParcelable("banner",banner);
                activity().toActivity(BannerActivity.class,extras);
            }
        });
        BannerHolder.align(cb_banner);

        rv_category.setHasFixedSize(true);
        rv_category.setNestedScrollingEnabled(false);
        rv_category.setLayoutManager(new GridLayoutManager(getContext(),4));
        CategoryAdapter adapter = new CategoryAdapter(getContext(),Category.categories());
        rv_category.setAdapter(adapter);
        adapter.setItemClickListener(new EListener<Category>() {
            @Override
            public void onInvoked(View view, Category data, int position) {
                activity().toActivity(CategoryActivity.class);
            }
        });

        String nearby = "http://picapi.ooopic.com/11/73/23/20b1OOOPICdd.jpg";
        Glide.with(this).load(nearby).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_nearby);


        rv_recommend.setHasFixedSize(true);
        EViewUtils.horizontal(rv_recommend);
        rv_recommend.setAdapter(new RecommendAdapter(getContext(),recommends()));

        rv_shop.setHasFixedSize(true);
        EViewUtils.horizontal(rv_shop);
        rv_shop.setAdapter(new ShopAdapter(getContext(),shops()));

        rv_favourable.setHasFixedSize(true);
        rv_favourable.setNestedScrollingEnabled(false);
        rv_favourable.setLayoutManager(new GridLayoutManager(getContext(),2));
        rv_favourable.setAdapter(new FavourableAdapter(getContext(),favourables()));
    }

    private List<Recommend> recommends(){
        List<Recommend> recommends = new ArrayList<>(10);
        for (int i = 0;i<10;i++){
            Recommend recommend = new Recommend();
            recommend.price = 2500;
            recommend.name = "HomePod";
            recommend.img = "http://f11.baidu.com/it/u=2627462197,4077403640&fm=175&s=03B0EA22D1E422BC5FB50C9F0100C0C0&w=640&h=426&img.JPEG&access=215967316";
            recommends.add(recommend);
        }

        return recommends;
    }

    private List<Shop> shops(){
        List<Shop> shops = new ArrayList<>(10);
        for (int i = 0;i<10;i++){
            Shop shop = new Shop();
            shop.name = "Apple";
            shop.distance = "1.3km";
            shop.img = "http://f11.baidu.com/it/u=2627462197,4077403640&fm=175&s=03B0EA22D1E422BC5FB50C9F0100C0C0&w=640&h=426&img.JPEG&access=215967316";
            shops.add(shop);
        }

        return shops;
    }

    private List<Favourable> favourables(){

        List<Favourable> favourables = new ArrayList<>(4);
        for (int i = 0;i<4;i++){
            Favourable favourable = new Favourable();
            favourable.name = "Apple";
            favourable.price = 2300;
            favourable.originalPrice = 2600;
            favourable.img = "http://f11.baidu.com/it/u=2627462197,4077403640&fm=175&s=03B0EA22D1E422BC5FB50C9F0100C0C0&w=640&h=426&img.JPEG&access=215967316";
            favourables.add(favourable);
        }

        return favourables;
    }

    @OnClick(R.id.iv_scanner)
    protected void onScannerClick(){
        activity().toActivity(ScannerActivity.class);
    }

    @OnClick(R.id.tv_search)
    protected void onSearchClick(){
        activity().toActivity(SearchActivity.class);
    }
    @Override
    public void onResume() {
        super.onResume();
        cb_banner.startTurning(5000);
    }

    @Override
    public void onStop() {
        super.onStop();
        cb_banner.stopTurning();
    }

    private static class BannerHolder implements Holder<Banner> {

        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, Banner data) {
            Glide.with(context).load(data.img).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
        }

        public static void align(ConvenientBanner cbBanner) {
            cbBanner.setPageIndicator(new int[]{R.drawable.sign_page_grey, R.drawable.sign_page_blue});
            cbBanner.setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);
        }
    }
}
